package com.example.bishe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bishe.entity.User;
import com.example.bishe.entity.UserRoom;
import com.example.bishe.mapper.UserMapper;
import com.example.bishe.mapper.UserRoomMapper;
import com.example.bishe.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.bishe.util.Encrypt;
import com.example.bishe.util.RedisUtil;
import com.example.bishe.util.xorEncrypt;
import com.example.bishe.web.b.vo.getUserPageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * <p>
 *  用户服务实现类
 * </p>
 *
 * @author LDH
 * @since 2022/11/30
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoomMapper userRoomMapper;
    @Autowired
    private RedisUtil redisUtil;

    /**
    * 功能描述: 用户登录
    * @Param: [openId]
    * @return: com.example.bishe.entity.User
    * @Author: LDH
    * @Date: 2023/04/16
    */
    @Override
    public User login(String openId) {
        String cipher = Encrypt.jiami(openId);
        User user = userMapper.getByOpenId(cipher);

        // 存入openid
        redisUtil.set("openId", xorEncrypt.setKey(openId));

        // 账号存在
        if (user != null) return user;
        // 账号不存在
        User newUser = new User();
        newUser.setOpenId(cipher);
        userMapper.insert(newUser);

        return userMapper.getByOpenId(cipher);
    }

    /**
    * @Description: 分页查询用户信息
    * @Param: [pageNum, pageSize, name, startTime, endTime, state, roomNum]
    * @return: com.baomidou.mybatisplus.core.metadata.IPage<com.example.bishe.web.b.vo.getUserPageVO>
    * @Author: LDH
    * @Date: 2023/02/05
    */
    @Override
    public IPage<getUserPageVO> userPage(int pageNum, int pageSize, Long id, String name, String startTime, String endTime, String state, Integer roomNum) {
        Page<getUserPageVO> page = new Page<>(pageNum,pageSize);
        return userMapper.getPage(page, id, name,startTime,endTime,state,roomNum);
    }

    /**
     * @Description: 将用户设置为启用状态
     * @Param: [id]
     * @return: void
     * @Author: LDH
     * @Date: 2023/02/05
     */
    @Override
    public void setON(Long id) {
        User user = userMapper.selectById(id);
        user.setState("ON");
        userMapper.updateById(user);
    }

    /**
     * @Description: 将用户设置为禁用状态
     * @Param: [id]
     * @return: void
     * @Author: LDH
     * @Date: 2023/02/05
     */
    @Override
    public void setOFF(Long id) {
        User user = userMapper.selectById(id);
        user.setState("OFF");
        userMapper.updateById(user);
    }

    /**
     * @Description: 删除用户
     * @Param: [id]
     * @return: void
     * @Author: LDH
     * @Date: 2023/02/05
     */
    @Override
    public void deleteOne(Long id) {
        // 删除空间内用户
        LambdaQueryWrapper<UserRoom> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserRoom::getUserId, id);
        userRoomMapper.delete(queryWrapper);

        // 删除用户
        baseMapper.deleteById(id);
    }

    /**
    * 功能描述: 批量删除用户
    * @Param: [users]
    * @return: void
    * @Author: LDH
    * @Date: 2023/05/09
    */
    @Override
    public void deleteBatch(List<User> users) {
        users.forEach(user -> {
            // 删除空间内用户
            LambdaQueryWrapper<UserRoom> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(UserRoom::getUserId, user.getId());
            userRoomMapper.delete(queryWrapper);

            // 删除用户
            baseMapper.deleteById(user.getId());
        });
    }

    /**
    * 功能描述: 修改用户头像及昵称
    * @Param: [id, avator, nikname]
    * @return: com.example.bishe.entity.User
    * @Author: LDH
    * @Date: 2023/04/16
    */ 
    @Override
    public User updateByAuth(Long id, String avator, String nikname) {
        User user = userMapper.selectById(id);
        user.setAvator(avator);
        user.setNikName(nikname);
        userMapper.updateById(user);
        return user;
    }
}
